package com.longtop.framework.sys.file.excel;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.longtop.framework.security.BaseUncheckedException;
import com.longtop.framework.util.StringUtil;

import edu.npu.fastexcel.FastExcel;
import edu.npu.fastexcel.Sheet;
import edu.npu.fastexcel.Workbook;
import edu.npu.fastexcel.FastExcel;
import edu.npu.fastexcel.biff.parser.ParserException;
import edu.npu.fastexcel.compound.io.ReadException;

public class ReadExcelInfo {

	public static List<String> findExcelHeadRow(String strPath, String readHeadfromRow) throws ParserException, ReadException, BaseUncheckedException {
		return readExcelRowData(new File(strPath), 0, readHeadfromRow);
	}

	public static List<String> findExcelHeadRow(File file, String readHeadfromRow) throws ParserException, ReadException, BaseUncheckedException {
		return readExcelRowData(file, 0, readHeadfromRow);
	}

	public static void cheakExcelFile(File tempFile, File uploadFile, String readHeadfromRow) throws BaseUncheckedException, ParserException, ReadException {
		// boolean bool = false;
		List<String> tempList = null;
		List<String> uploadList = null;
		try {
			tempList = findExcelHeadRow(tempFile, readHeadfromRow);
			uploadList = findExcelHeadRow(uploadFile, readHeadfromRow);
		} catch (Exception e) {
			throw new BaseUncheckedException("上传的EXCEL文件与模板文件不匹配 请检查重新上传。");
		}
		if (tempList.size() != uploadList.size()) {
			throw new BaseUncheckedException("上传的EXCEL文件与模板文件不匹配 请检查重新上传。");
		} else {
			if (!uploadList.containsAll(tempList)) {
				throw new BaseUncheckedException("上传的EXCEL文件与模板文件不匹配 请检查重新上传。");
			}
		}
	}

	public static List<String> readExcelRowData(File file, int sheetIndex, String readHeadfromRow) throws ReadException, BaseUncheckedException, ParserException {
		Workbook workBook = FastExcel.createReadableWorkbook(file);
		workBook.open();

		Sheet sheet = workBook.getSheet(sheetIndex);
		List<String> listTemp = new ArrayList<String>();
		for (int j = sheet.getFirstColumn(); j < sheet.getLastColumn(); j++) {
			String cellTemp = StringUtil.getString(sheet.getCell(Integer.valueOf(readHeadfromRow), j));
			if (!cellTemp.equals("")) {
				listTemp.add(sheet.getCell(Integer.valueOf(readHeadfromRow), j));
			}
		}
		if (listTemp.size() < 1) {
			throw new BaseUncheckedException("EXCEL读写错误,请检查模板EXCEL的配置内容");
		}
		workBook.close();
		return listTemp;
	}

	public static List<String> readExcelHeadData(File file, String readHeadfromRow) throws ReadException, BaseUncheckedException, ParserException {
		Workbook workBook = FastExcel.createReadableWorkbook(file);
		workBook.open();

		Sheet sheet = workBook.getSheet(0);
		List<String> listTemp = new ArrayList<String>();
		for (int j = sheet.getFirstColumn(); j < sheet.getLastColumn(); j++) {
			String cellTemp = StringUtil.getString(sheet.getCell(Integer.valueOf(readHeadfromRow), j));
			if (!cellTemp.equals("")) {
				String strTemp = sheet.getCell(Integer.valueOf(readHeadfromRow), j);
				// 这里解析表头，在导出模板里如果存在“[ ]”如”员工号[userId]“；则解析的时候存入的是userId，否则存入整个表头字符串。
				if (strTemp.indexOf("[") != -1) {
					strTemp = strTemp.substring(strTemp.indexOf("[") + 1);
					if (strTemp.indexOf("]") != -1) {
						strTemp = strTemp.substring(0, strTemp.length() - 1);
					}
				}
				listTemp.add(strTemp);
			}
		}
		if (listTemp.size() < 1) {
			throw new BaseUncheckedException("EXCEL读写错误,请检查模板EXCEL的配置内容");
		}
		workBook.close();
		return listTemp;
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> readExcelData(File file, Class clz, String readHeadfromRow) throws ReadException, BaseUncheckedException, ParserException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Workbook workBook = FastExcel.createReadableWorkbook(file);
		workBook.open();
		List<T> listTemp = new ArrayList<T>();
		List<String> listHead = readExcelHeadData(file, readHeadfromRow);

		Sheet sheet = workBook.getSheet(0);
		for (int jj = sheet.getFirstRow() + 1 + Integer.parseInt(readHeadfromRow); jj < sheet.getLastRow(); jj++) {
			Map excelMap = new HashMap();
			List<String> listRow = new ArrayList<String>();
			for (int j = sheet.getFirstColumn(); j <= sheet.getLastColumn(); j++) {
				String cellTemp = StringUtil.getString(sheet.getCell(jj, j));
				if (!cellTemp.equals("")) {
					listRow.add(sheet.getCell(jj, j));
					excelMap.put(listHead.get(j), sheet.getCell(jj, j));
				}
			}
			listTemp.add((T) excelMap);

		}
		List<T> listObject = new ArrayList<T>();

		if (listTemp.size() > 0 && clz != null) {

			for (int i = 0; i < listTemp.size(); i++) {
				T t = (T) clz.newInstance();
				BeanUtils.copyProperties(t, listTemp.get(i));
				listObject.add(t);
			}

			listTemp.clear();
			listTemp.addAll(listObject);
		}
		workBook.close();
		return listTemp;
	}
}
